using System.Diagnostics;

namespace ProjectEuler
{
    internal class Problem27
    {
        public void Solve()
        {
            var abslimit = 1000;
            int maxA = -(abslimit - 1);
            int maxB = -(abslimit - 1);
            int maxN = 0;
            var a = maxA;
            var b = maxB;
            while (a < abslimit)
            {
                while (b < abslimit)
                {
                    var num = CheckNumPrimes(a, b);
                    if (num > maxN)
                    {
                        maxN = num;
                        maxA = a;
                        maxB = b;
                    }

                    b++;
                }

                a++;
                b = -(abslimit - 1);
            }


            Debug.WriteLine("a = {0}, b = {1}, n = {2}, prod = {3}", maxA, maxB, maxN, maxA*maxB);
        }

        int CheckNumPrimes(int a, int b)
        {
            var n = 0;
            var isPrime = Common.IsPrime(Quadratic(n, a, b));
            if (!isPrime) return 0;

            while (isPrime)
            {
                n++;
                isPrime = Common.IsPrime(Quadratic(n, a, b));
            }

            return n;
        }

        int Quadratic(int n, int a, int b)
        {
            return n*n + a*n + b;
        }
    }
}